
//房间列表

/**
 * 随机生成房间
 */
function createRoom(x,y,width,height){
    
   

    var tx=getRamdom(0,MAX_X);
   
    if(!x||!y){
        var ty=getRamdom(0,emptyList[tx].length-1);
        var sSqu=emptyList[tx][ty];

        var rx=sSqu.x;
        var ry=sSqu.y;
    }else{
        rx=x;
        ry=y;
    }

   
  

    var rWidth=width?width:getRamdom(minW,maxW);
    var rHeight=height?height:getRamdom(minH,maxH);

    if(rx+rWidth>MAX_X||ry+rHeight>MAX_Y){
        return '';
    }

    var room= new Rec(rx,ry,rWidth,rHeight);
    

    if(room.inValid()){
        return "";
    }else{
        
           
         for(var i=room.x;i<room.x+room.width;i++){

            for(var j=room.y;j<room.y+room.height;j++){
                var temp=getSqu(i,j);
                temp.filled=true;
                
                var tempIndex=emptyList[room.x].indexOf(temp);
                emptyList[room.x].splice(tempIndex,1);
                filledList.push(temp)

            }
  
        };
       
        return room;
    }

}


function creRoom(){
    var tx=getRamdom(0,emptyList.length-1);
    if(emptyList[tx].length==0){
        emptyList.splice(tx,1);
        return "";
    }
    var ty=getRamdom(0,emptyList[tx].length-1);

    var sSqu=emptyList[tx][ty];

    var rx=sSqu.x;
    var ry=sSqu.y;
    var rWidth,rHeight;
    
   
    var temp=sSqu;
    while(temp.x-rx<maxW){
        if(temp.x>=MAX_X||temp.farFilled()){
            break;
        }else{
             temp=getSqu(temp.x+1,temp.y)
        }
       
    }
    var recMaxW=temp.x-rx;
    if(recMaxW<minW){
        return "";
    }else{
        rWidth=getRamdom(minW,recMaxW)
    }
    
    var temp=sSqu;
    while(temp.y-ry<maxH){
        if(temp.y>=MAX_Y||temp.farFilled()){
            break;
        }else{
            temp=getSqu(temp.x,temp.y+1)
        }
        
    };
    var recMaxH=temp.y-ry;
    if(recMaxH<minH){
        return ""
    }else{
        rHeight=getRamdom(minH,recMaxH)
        if(!rHeight){
            console.log(recMaxH+';'+temp.y+';'+ry)
        }
    };

    var color=color?color:colorList[getRamdom(0,colorList.length)]

    var room= new Rec(rx,ry,rWidth,rHeight,color);
    
    
    if(room.inValid()){
        return "";
    }else{
            
         for(var i=room.x;i<room.x+room.width;i++){

            for(var j=room.y;j<room.y+room.height;j++){
                var temp=getSqu(i,j);
                temp.filled=true;
                if(!emptyList[room.x]){
                   continue;
                }
                
                var tempIndex=emptyList[room.x].indexOf(temp);
                emptyList[room.x].splice(tempIndex,1);
                filledList.push(temp)

            }
  
        };
    }
    // console.log(room)
    return room;
}